package com.jinglu.cloud.security.utils;

import com.jinglu.cloud.security.model.AuthInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

/**
 * @Author lvzhenzhao
 * @Datetime 2021/03/02-下午3:22
 */
public class UserUtil {

    private static final Logger logger = LoggerFactory.getLogger(UserUtil.class);

    public UserUtil() {
    }

    public static AuthInfo getUserInfo() {
        try {
            Object object = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (Objects.nonNull(object)) {
                logger.info("获取当前用户{}", object);
            }

            AuthInfo userDetails = (AuthInfo) object;
            if (userDetails != null) {
                return userDetails;
            }
        } catch (Exception var2) {
            logger.info("获取当前用户失败,message={}", var2.getMessage());
        }

        return null;
    }
}
